AlDanial / cloc

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
GNU General Public License v2.0
19.42k stars 1.02k forks source link

cloc --git-diff-rel does not respond correctly count #799

Closed houwen0 closed 9 months ago

houwen0 commented 9 months ago

Describe the bug cloc counts respond incorrectly count when counting diffs between 2 commits

cloc; OS; OS version

To Reproduce

  1. cloc-1.98 --diff 39a54ce3 66ec3dfe

  2. the result image

  3. refer to the commit different of gitlab

image image

Expected result I expect the modified code to be 7, the added code to be 1 or 0,the removed code to be 1.

AlDanial commented 9 months ago

I'll need the two versions of these files to reproduce the problem. Can you post them?

houwen0 commented 9 months ago

The above screenshot is the commit provided by Gitlab, and I can provide you with these two source files:

1、SsoController

package com.test.svc.web.controller;

import com.test.svc.common.constant.ServiceExceptionCode; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.test.framework.auditlog.expection.ServiceException; import top.test.framework.auth.client.UnifyAuthContextHolder; import top.test.framework.auth.client.annotation.Authorize; import top.test.framework.auth.facade.authrization.AuthContext; import top.test.framework.auth.facade.constant.AuthConstant; import top.test.framework.auth.facade.entity.UserInfo; import top.test.framework.biz.ro.TestResult; import javax.servlet.http.HttpServletRequest;

@RestController @RequestMapping("/sso") public class SsoController {

@GetMapping("/authorize")
@Authorize()
public TestResult<String> authorize(HttpServletRequest request) {
    AuthContext authContext = (AuthContext) request.getServletContext().getAttribute(AuthConstant.AUTH_CONTEXT_KEY);
    return TestResult.success(ServiceExceptionCode.SUCCESS.code,"process success",authContext.getUserInfo().getUsername());
}

@GetMapping("/unauthorize")
public TestResult<String> unAuthorize( ) {
    return TestResult.success(ServiceExceptionCode.SUCCESS.code,"process success","No verification is required !");
}

@GetMapping("/authorize/userInfo")
@Authorize()
public TestResult<UserInfo> authorizeUserInfo(HttpServletRequest request) {
    UserInfo userInfo  = UnifyAuthContextHolder.getUserInfo();

// AuthContext authContext = (AuthContext) request.getServletContext().getAttribute(AuthConstant.AUTH_CONTEXT_KEY); return TestResult.success(ServiceExceptionCode.SUCCESS.code,"process success",userInfo); }

}

2、SysInfoController

package com.test.svc.web.controller.svc_govern;

import com.test.svc.common.constant.ServiceExceptionCode; import com.test.svc.core.service.impl.svc_govern.SysInfoServiceImpl; import org.springframework.web.bind.annotation.*; import top.test.framework.auth.client.annotation.Authorize; import top.test.framework.biz.ro.TestResult; import javax.annotation.Resource; import com.test.svc.dal.po.svc_govern.mbg.SysInfo; import top.test.framework.biz.ro.PagedTestResult;

/* */ @RestController @RequestMapping("/sysInfo") public class SysInfoController {

@Resource
private SysInfoServiceImpl sysInfoService;

/**
* 新增
*/
@PostMapping("")
@Authorize(roles = {},authorities ={})
public TestResult insert(@RequestBody SysInfo sysInfo){
    sysInfoService.insert(sysInfo);
    return TestResult.success(ServiceExceptionCode.SUCCESS.code,null,null);
}

/**
* 更新
*/
@PutMapping("")
@Authorize(roles = {},authorities ={})
public TestResult<String> update(@RequestBody SysInfo sysInfo){
    sysInfoService.update(sysInfo);
    return TestResult.success( ServiceExceptionCode.SUCCESS.code,null,null);
}

/**
* Load查询
*/
@GetMapping("/{id}")
@Authorize(roles = {},authorities ={})
public TestResult<SysInfo> load(@PathVariable long id){
    return TestResult.success(ServiceExceptionCode.SUCCESS.code,null, sysInfoService.load(id));
}

@GetMapping("/pageShow/pageNum/{pageNum}/pageSize/{pageSize}")
@Authorize(roles = {},authorities ={})
public PagedTestResult<SysInfo> page(@PathVariable int pageNum, @PathVariable int pageSize){
    return  sysInfoService.showPage(pageNum,pageSize);
}

}

houwen0 commented 9 months ago

I am not sure if the “git diff” is a usage issue or if the function is incomplete. If there are any usage issues, please tell me the correct method

AlDanial commented 9 months ago

I don't think the content you posted is consistent with the results you showed. I have attached the files as you provided them. These two files have little in common. When I run cloc --diff SsoController.java SysInfoController.java I get

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Java
 same                            0              0              0              8
 modified                        1              0              1             19
 added                           0              4             11             10
 removed                         0              0              0              6
-------------------------------------------------------------------------------
SUM:
 same                            0              0              0              8
 modified                        1              0              1             19
 added                           0              4             11             10
 removed                         0              0              0              6
-------------------------------------------------------------------------------

If you attach the files rather than cut/paste their contents it will be easier to work with them. SysInfoController.java.txt SsoController.java.txt

houwen0 commented 9 months ago

sorry,What I am comparing is not these two files, but the two commit attempts of Gitlab。The command I executed is as follows: cloc-1.98 --git-diff-rel 39a54ce3 66ec3dfe

"39a54ce3" or "66ec3dfe" is gitlab's commit id

AlDanial commented 9 months ago

Based on your screenshot, your commits refer to two files each. When you tell cloc to diff the two commits you are really saying "compare the files between these two commits". To reproduce this problem I'll either need to clone your git repo or to get the four files themselves.

houwen0 commented 9 months ago

Sorry, the real files are confidential. I'm wondering if you support the "git diff" feature. I want to use this feature to count the changes made in a git commit, such as additions, modifications, and deletions.

AlDanial commented 9 months ago

cloc uses internal logic to compute diffs between two commits (the output from git diff by itself is not sufficient to compute comment and code changes), so yes, it does support the capability you want.

However, without the input files, I won't be able to tell why the results are not as you expect. If you encounter this problem with files you are able to distribute, please open a new issue.